<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 15:22
 */
namespace app\shop\controller;
use think\Db;
class Reasons extends Base
{
    /**
     * 拒绝原因
     * @return mixed|\think\response\Json
     */
    public function reasons(){
        extract(input());
        $search = input('param.search/a');
        $page = input('param.page');
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }
        $search = $this->search($search, $page);
        //搜索条件
        $map = [];
        //任务名称
        if($search[0] != " "){
            $map['reason_name'] = ['like',"%" . $search[0] . "%"];
        }
        $map['reason_shop_id'] = session('shop_id');
        //1、列表
        $data = Db::table('up_shop_reason')
            ->where($map)
            ->order('reason_time DESC')
            ->paginate(25);
        return $this->fetch ('index/reasons',['data'=>$data,'pages'=>$pages,'search'=>$search]);
    }


    /**
     * 添加或编辑
     * @return \think\response\Json
     */
    public function addOrEdit()
    {
        extract(input());
        if(empty($reason)){
            return json(['code'=>100,'msg'=>'拒绝原因不能为空！']);
        }
        $today = date('Y-m-d H:i:s');
        if($reason_id && $reason_id > 0){
            $res = Db::table('up_shop_reason')->where('reason_id',$reason_id)->update([
                'reason_name' => $reason,
                'reason_time' => $today
            ]);
        }else{
            $insert = [
                'reason_name' => $reason,
                'reason_shop_id' => session('shop_id'),
                'reason_time' => $today,
            ];
            $res = Db::table('up_shop_reason')->insert($insert);
        }
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    /**
     * 上架、下架、删除
     * @return \think\response\Json
     */
    public function upDownDel()
    {
        extract(input());
        if($type != 1 && $type != 2 && $type != 3){
            return json(['code'=>100,'msg'=>'传参错误！']);
        }
        $today = date('Y-m-d H:i:s');
        if($type != 3){
            $res = Db::table('up_shop_reason')->where('reason_id',$reason_id)->update([
                'reason_status' => $type,
                'reason_time' => $today
            ]);
        }else{
            $res = Db::table('up_shop_reason')->where('reason_id',$reason_id)->delete();
        }
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
}